/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

/**
 *
 * @author Young Ran
 */
import entity.SuccessStory;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Young Ran
 */
public class SuccessStoryDAO {
    
    private Connection cn;
    private PreparedStatement pstm;
    private CallableStatement cs;
    
    public SuccessStoryDAO() {
        cn = DB_Util.getConnection();
    }
    
    private ArrayList getSuccessStoryData(ResultSet rs) throws SQLException {
        ArrayList list = new ArrayList();
        while (rs.next()) {
            SuccessStory e = new SuccessStory();
            e.setId(rs.getInt("SuccessStoryId"));
            e.setName(rs.getString("SuccessStoryName"));
            e.setStory(rs.getString("SuccessStoryStory"));
            list.add(e);
        }
        return list;
    }
    
    //DISPLAY SUCCESS STORIES:
    public ArrayList getAllSuccessStory() throws SQLException {
        cs = cn.prepareCall("{call SP_Display_SuccessStory}");
        ResultSet rs = cs.executeQuery();
        return getSuccessStoryData(rs);
    }
        public void addSuccessStory(SuccessStory item) throws SQLException
        {
        cs = cn.prepareCall("{call SP_Add_SuccessStory(?,?)}");
        cs.setNString(1, item.getName());
        cs.setNString(2, item.getStory());
        cs.execute();
        }
        public void editSuccessStory(SuccessStory item) throws SQLException
        {
        cs = cn.prepareCall("{call SP_Edit_SuccessStory(?,?,?)}");
        cs.setInt(1, item.getId());
        cs.setNString(2, item.getName());
        cs.setNString(3, item.getStory());
        cs.execute();
        }
        public void deleteSuccessStory(int id) throws SQLException
        {
        cs = cn.prepareCall("{call SP_Delete_SuccessStory(?)}");
        cs.setInt(1,id);
        cs.execute();
        }
}